package com.kstudio.videocapture.util;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public final class KUtil {

    public static Bitmap loadBitmap(Resources res, int id, int width, int height) {
        return Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, id), width, height, false);
    }

    public static Bitmap loadBitmap(String filename) {
        Bitmap bitmap = null;

        try {
            bitmap = BitmapFactory.decodeFile(filename);
        } catch(OutOfMemoryError e) {
            boolean finish = true;
            int inSampleSize = 1;
            BitmapFactory.Options options = new BitmapFactory.Options();

            while(finish) {
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(filename, options );

                try{
                    inSampleSize++;
                    options.inSampleSize = inSampleSize;
                    options.inJustDecodeBounds = false;

                    bitmap = BitmapFactory.decodeFile(filename, options);
                } catch(OutOfMemoryError e1) {
                    Log.e("KUtil",String.format("LoadBitmap w = %d , h = %d , inSampleSize = %d", options.outWidth, options.outHeight , inSampleSize));
                }
                finish = false;
            }
        }

        return bitmap;
    }

    public static Bitmap loadBitmapResizedOnRatio(String filename, int width, int height ) {
        Bitmap bitmap = loadBitmap(filename);
        Bitmap b = null;

        if ( bitmap != null ) {
            b = resizedBitmapOnRatio(bitmap, width, height);
            bitmap.recycle();
        }

        return b;
    }

    public static Bitmap fastLoadBitmapResizedOnRatio(String filename, int width, int height ) {
        Log.i("KUtil","fastLoadBitmapResizedOnRatio file name = "+filename);
        int inSampleSize = 1;
        BitmapFactory.Options options = new BitmapFactory.Options();

        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filename, options );

        double ratioWidth  = (double) options.outWidth  / (double) width;
        double ratioHeight = (double) options.outHeight / (double) height;

        if(ratioWidth > ratioHeight) {
            inSampleSize = (int) ratioWidth;
        } else {
            inSampleSize = (int) ratioHeight;
        }

        options.inSampleSize = inSampleSize;
        options.inJustDecodeBounds = false;

        Bitmap bmTemp = BitmapFactory.decodeFile(filename, options);
        Bitmap Result = null;
        if(bmTemp != null) {
            if(bmTemp.getWidth() != width && bmTemp.getHeight() != height) {
                Result = KUtil.resizedBitmapOnRatio(bmTemp, width, height);
                bmTemp.recycle();
            } else {
                Result = bmTemp;
            }
        }

        return Result;
    }

    public static Bitmap resizedBitmapOnRatio(Bitmap bitmap, int width, int height) {
        int modifyWidth = width;
        int modifyHeight = height;
        Bitmap b = null;

        if(bitmap != null) {
            if(bitmap.getWidth() >= bitmap.getHeight()) {
                modifyHeight = (int) ((double) modifyWidth * (double) bitmap.getHeight() / (double) bitmap.getWidth());
            } else {
                modifyWidth = (int) ((double) modifyHeight * (double) bitmap.getWidth() / (double) bitmap.getHeight());
            }
            if(bitmap.getWidth() == modifyWidth && bitmap.getHeight() == modifyHeight) {
                b = bitmap.copy(Bitmap.Config.ARGB_8888,true);
            } else {
                b = Bitmap.createScaledBitmap(bitmap, modifyWidth, modifyHeight, false);
            }
        }

        return b;
    }

    public static Bitmap resizedBitmapOnFit(Bitmap bm, int width, int height) {
        Bitmap Result = Bitmap.createScaledBitmap(bm, width, height, false);
        bm.recycle();

        return Result;
    }
}